package com.kermitye.studentsys;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class DetailActivity extends AppCompatActivity {


    LinearLayout mLlNo;
    TextView mTvTitle, mTvNo;
    EditText mEtName, mEtSpeciality, mEtClazz;
    Button mBtnSave;
    String mType;
    Student mData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        initView();
    }


    private void initView() {
        mLlNo = findViewById(R.id.ad_ll_no);
        mTvTitle = findViewById(R.id.ad_tv_title);
        mTvNo = findViewById(R.id.ad_tv_no);
        mEtName = findViewById(R.id.ad_et_name);
        mEtSpeciality = findViewById(R.id.ad_et_speciality);
        mEtClazz = findViewById(R.id.ad_et_clazz);
        mBtnSave = findViewById(R.id.ad_btn_save);

        mType = getIntent().getStringExtra("type");
        if (mType.equals("add")) {
            mLlNo.setVisibility(View.GONE);
            mTvTitle.setText("添加");
        } else {
            mTvTitle.setText("编辑");
            mData = (Student) getIntent().getSerializableExtra("data");
            mTvNo.setText(mData.id+"");
            mEtName.setText(mData.name);
            mEtSpeciality.setText(mData.speciality);
            mEtClazz.setText(mData.clazz);
        }
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
    }

    private void save() {
        String name = mEtName.getText().toString();
        String speciality = mEtSpeciality.getText().toString();
        String clazz = mEtClazz.getText().toString();
        if (name.isEmpty()) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (speciality.isEmpty()) {
            Toast.makeText(this, "专业不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if (clazz.isEmpty()) {
            Toast.makeText(this, "班级不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (mType.equals("add")) {
            Student student = new Student();
            student.name = name;
            student.speciality = speciality;
            student.clazz = clazz;
            MyService.instance.dbManager.add(student);
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            mData.name = name;
            mData.speciality = speciality;
            mData.clazz = clazz;
            MyService.instance.dbManager.update(mData);
            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

}
